<?php
/**
 * 酒店管理
 * User: lxq
 * Date: lxq
 * Time: 17:19
 */
class hotelControl  extends BaseHomeControl {

    public function __construct()
    {
        if($_GET['op']=='place_info' || $_GET['op']=='sel_info'){
            $this->_must_login = true;
        }
        $this->update_order_status();
        parent::__construct();
        Tpl::setDir('hotel');
        Tpl::setLayout('new_layout');
    }

    /**
     * 酒店列表
     */
    public function indexOp()
    {
        //$_SESSION['location'] = array('lat'=>'24.483720','lng'=>'118.180950');
        if($_SESSION['location']){
        $location = $_SESSION['location'];
        $field = '*';
        //$distance = 'SQRT(pow('. $location['lat'] .' - hotel_lat, 2) + pow('. $location['lng'] .'  - hotel_lng, 2)) AS distance';
        $distance = 'SQRT(pow('. $location['lng'] .' - hotel_lat, 2) + pow('. $location['lat'] .'  - hotel_lng, 2)) AS distance';
        $field .= ','.$distance;
    }

        //$order = 'distance asc';
        $condition = array();
        $condition['ht_status'] = 1;
        /* @var hotelModel $hotel_model */
        $hotel_model = Model('hotel');
        $area_model  = Model('area');
        if($cate_id = $_GET['cate_id']){
            $condition['cate_id'] = intval($cate_id);//分类ID
        }
        if($area_id = $_GET['area']){
            $condition['ht_area_id'] = $area_id; //地区ID
        }
        if($keyword = trim($_GET['keyword'])){
            $condition['hotel_name'] = array('like','%'.$keyword.'%');
        }
        if($price=$_GET['price']){
            $order = 'ht_market_price '.$price;
        }else{
            if($_SESSION['location']){
                $order = 'distance asc';
            }
        }
        $category_model = Model('ticket_category');
        $list = $hotel_model->getHotelListPage($condition,$field,5,$order);
        if($list && is_array($list)){
            foreach ($list as $key =>$value){
                $list[$key]['hotel_thumb'] = getGoodsImg($value['hotel_thumb'],HOTEL_MARK);
                if($value['distance']){
                    $distance = $value['distance'];
                    $distance = round($distance * 10000) / 100;
                    if($distance<0.1){
                        $list[$key]['distance']='小于100m';
                    }else{
                        $list[$key]['distance'] = $distance."km"; // KM
                    }
                }
            }
        }
        Tpl::output('data',$list);
        //AJAX加载分页
        if(isset($_GET['isajax']) && $_GET['isajax'] == 1){
            Tpl::showpage('hotel.index.page','null_layout');
            exit;
        }
        $cityID  = $hotel_model->getHotelAreaId();//地区列表
        if($cityID){
            $area_arr = array();
            foreach($cityID as $item =>$value){
                $cityID[$item]['name'] = $area_model->getfby_id($value['ht_area_id']);
            }
            Tpl::output('area_arr',$cityID);
        }
        //酒店底下所有分类
        $cate = $category_model->getCategoryList(array('tc_pid'=>HOTEL_MARK,'is_del'=>0));
        Tpl::output('get_data',$_GET);
        Tpl::output('hotel_time',$_SESSION['hotel_time']);//入驻时间
        Tpl::output('TotalNum',pagecmd("gettotalnum"));
        Tpl::output('TotalPage',pagecmd("gettotalpage"));
        Tpl::output('cate',$cate);
        Tpl::showpage('hotel.index');
    }

    /**
     * 酒店搜索页
     *
     */
    public function hotel_searchOp()
    {
        $condition = array();
        $condition['ht_status'] = 1;
        /* @var hotelModel $hotel_model */
        $hotel_model = Model('hotel');
        $fields = implode(',',array(
            'id',
            'hotel_name',
            'hotel_thumb',
            'ht_market_price',
            'hotel_point',
            'ht_full_addr',
        ));
        if($cate_id = $_GET['cate_id']){
            $condition['cate_id'] = intval($cate_id);//分类ID
        }
        if($area_id = $_GET['area']){
            $condition['ht_area_id'] = $area_id; //地区ID
        }
        if($keyword = trim($_GET['keyword'])){
            $condition['hotel_name'] = array('like','%'.$keyword.'%');
        }
        //排序
        $column	 = $_GET['column'];
        $order	 = strtolower($_GET['order']) == 'desc' ? 'desc' : 'asc';
        $column	 = empty($column) ? 'ht_update_time ' : $column.' ';

        $list = $hotel_model->getHotelListPage($condition,$fields,5,$column.$order);
        Tpl::output('data',$list);
        //var_dump($list);
    }

    /**
     * 酒店详情页面
     */
    public function hotel_infoOp()
    {
        $hotel_id = intval($_GET['hotel_id']);
        /* @var hotelModel $hotel_model */
        $hotel_model = Model('hotel');
        /* @var specModel $spec_model */
        $spec_model = Model('spec');
        /* @var order_hotelModel $order_hotel_model */
        $order_hotel_model = Model('order_hotel');
        if($hotel_id>0){
            $condition = array();
            $condition2 = array();
            $_where =array();
            $_where['startdate'] = array('elt',$_SESSION['hotel_time']['enddate']);
            $_where['enddate'] = array('gt',$_SESSION['hotel_time']['startdate']);
            $_where['status'] = 1;
            $condition['goods_id'] = $hotel_id;
            $condition['type'] = HOTEL_MARK;
            $condition['parent_id'] = 0;
            $condition['is_show'] = 1;
            $hotel_info =  $hotel_model->getHotelInfo(array('id'=>$hotel_id,'ht_status'=>1));
            $hotel_info['hotel_thumb'] = getGoodsImg($hotel_info['hotel_thumb'],HOTEL_MARK);
            if($_GET['type']=='facility'){
                Tpl::output('hotel_info',$hotel_info);
                Tpl::showpage('facility.info');exit;
            }
            /** 查询用户筛选的这个时间段有没有属于该酒店关闭的房型 */
            /* @var hotel_dateModel $date_model */
            $date_model = Model('hotel_date');
            $condition2['date'] = array('between',array($_SESSION['hotel_time']['startdate'],date("Y-m-d",(strtotime($_SESSION['hotel_time']['enddate']) - 3600*24))));
            $condition2['ht_id'] = $hotel_id;
            $date_list = $date_model->getHotelDateList($condition2);
            /**  ---------------- */
            $spec_list = $spec_model->getSpecListPage($condition);
            foreach($spec_list as $key => $value){
                $_where['spec_id'] = $value['spec_id'];
                $spec_list[$key]['surface_img'] = getGoodsImg($value['surface_img'],HOTEL_MARK,'single');
                $hotel_oh_num = $order_hotel_model->getOrderHoteloh_num($_where);
                $spec_list[$key]['img_sum'] = Model('spec_images')->getSpecImageCount(array('spec_id'=>$value['spec_id'],'img_deleted'=>0));
                if($hotel_oh_num['sum_on_num']>0){
                    $spec_list[$key]['sold_num'] = $hotel_oh_num['sum_on_num'];
                }else{
                    $spec_list[$key]['sold_num'] = 0;
                }
                //判断是否开启规格
                if($value['open_spec']==1){
                    //获取该房型底下的规格信息
                    $field = 'spec_id,spec_name,market_price,price,parent_id,label';
                    $spec_list[$key]['find_list'] =  $spec_model->getSpecList(array('parent_id'=>$value['spec_id'],'is_show'=>1),$field);
                }
                /** 判断户筛选的这个时间段是否存在存在关闭的房型 */
                if($date_list && is_array($date_list)){
                    foreach($date_list as $kk => $vv ){
                        $arr = explode('#',$vv['spec_id']);
                        if(in_array($value['spec_id'],$arr)){
                            $spec_list[$key]['close'] = 1;
                            break;
                        }
                    }
                }
            }
            $images_model = Model('ticket_images');
            //获取酒店图册
            $images = $images_model->getTicketImageList(array('tk_id'=>$hotel_id, 'img_deleted'=>0,'type'=>HOTEL_MARK));
            if($images && is_array($images)){
                foreach($images as $k=>$v){
                    $images[$k]['img_path'] = getGoodsImg($v['img_path'],HOTEL_MARK,'single');
                }
            }
            //获取酒店的评论条数
            /* @var commentModel  $comment_model*/
            $comment_model = Model('comment');
            $cot_count = $comment_model->getCommentCount(array('goods_id'=>$hotel_id,'cmt_type'=>HOTEL_MARK));
            if($cot_count>0){
                //获取两条评论
                $cmt_list = $comment_model->getCommentPage(array('goods_id'=>$hotel_id,'cmt_type'=>HOTEL_MARK),'*',2);
                if($cmt_list && is_array($cmt_list)){
                    foreach($cmt_list as $key =>$value){
                        if($value['pic_1']){
                            $cmt_list[$key]['pic_1'] = UPLOAD.'comment/'.$value['pic_1'];
                        }
                        if($value['pic_2']){
                            $cmt_list[$key]['pic_1'] = UPLOAD.'comment/'.$value['pic_2'];
                        }
                        if($value['pic_3']){
                            $cmt_list[$key]['pic_1'] = UPLOAD.'comment/'.$value['pic_3'];
                        }
                        $cmt_list[$key]['member_name'] = nickname_hide($value['member_name']);
                    }
                    Tpl::output('cmt_list',$cmt_list);
                }
            }
            Tpl::output('cot_count',$cot_count);
            Tpl::output('images',$images);
            Tpl::output('spec_list',$spec_list);//房型列表
            Tpl::output('hotel_info',$hotel_info);//酒店详情
            Tpl::output('hotel_time',$_SESSION['hotel_time']);//入驻时间
            Tpl::showpage('hotel.info');
        }
    }

    /**
     * 修改酒店预定时间
     */
    public function edit_check_timeOp()
    {
        $this->edit_check_time();
    }

    /**
     * 获取房型详情页
     */
    public function get_roomOp()
    {
        $spec_id = intval($_GET['spec_id']);
        $spec_model = Model('spec');
        $spec_img_model = Model('spec_images');
        //房型详情
        $spec_info = $spec_model->getSpecInfo(array('spec_id'=>$spec_id));
        $spec_imgs = $spec_img_model->getSpecImageList(array('spec_id'=>$spec_id,'img_deleted'=>0),'img_path');
        $spec_info['img_sum']  = $spec_img_model->getSpecImageCount(array('spec_id'=>$spec_id,'img_deleted'=>0));
        if($spec_imgs){
            foreach($spec_imgs as $key => $value){
                $spec_imgs[$key]['img_path'] = getGoodsImg($value['img_path'],HOTEL_MARK,'single');
            }
        }
        //判断该房型是否有开启规格
        if($spec_info['open_spec']==0){
            //计算价格
            $all_price = $spec_info['price']*$_SESSION['hotel_time']['numdate'];
            Tpl::output('all_price',$all_price);
        }
        if($_GET['type']=='order'){
            Tpl::output('type',1);
            Tpl::output('spec_info',$spec_info);
            Tpl::output('spec_imgs',$spec_imgs);
            Tpl::showpage('room_info','null_layout');
            exit;
        }
        //判断用户当前选的日期是否可以购买
        /* @var order_hotelModel $order_hotel_model */
        $order_hotel_model = Model('order_hotel');
        $_where =array();
        $_where['startdate'] = array('elt',$_SESSION['hotel_time']['enddate']);
        $_where['enddate'] = array('gt',$_SESSION['hotel_time']['startdate']);
        $_where['status'] = 1;
        $_where['spec_id'] = $spec_id;
        $hotel_oh_num = $order_hotel_model->getOrderHoteloh_num($_where);
        if($hotel_oh_num['sum_on_num']>0){
           Tpl::output('sum_on_num',$hotel_oh_num['sum_on_num']);
        }else{
            Tpl::output('sum_on_num',0);
        }
        //判断用户选择的时间是否可以购买
        $date_model = Model('hotel_date');
        $condition2['date'] = array('between',array($_SESSION['hotel_time']['startdate'],date("Y-m-d",(strtotime($_SESSION['hotel_time']['enddate']) - 3600*24))));
        $condition2['ht_id'] = $spec_info['goods_id'];//酒店ID
        $date_list = $date_model->getHotelDateList($condition2);
        if($date_list  && is_array($date_list)){
            foreach($date_list as $kk => $value ){
                $arr = explode('#',$value['spec_id']);
                if(in_array($spec_id,$arr)){
                    $spec_info['close'] = 1;
                    break;
                }
            }
            //var_dump($spec_info);exit;
        }

        Tpl::output('spec_info',$spec_info);
        Tpl::output('spec_imgs',$spec_imgs);
        Tpl::output('hotel_time',$_SESSION['hotel_time']);
        Tpl::showpage('room_info','null_layout');
    }

    /**
     * 酒店图册
     */
    public function hotel_imagesOp()
    {
        $hotel_id = intval($_GET['ht_id']);
        /* @var ticket_imagesModel $images_model */
        $images_model = Model('ticket_images');
        $images = $images_model->getTicketImageList(array('tk_id'=>$hotel_id, 'img_deleted'=>0,'type'=>HOTEL_MARK));
        foreach ($images as $k => $img) {
            $images[$k]['img_path'] = UPLOAD . 'hotel/' . $img['img_path'];
        }
        Tpl::output('images',$images);
        Tpl::showpage('hotel.images');
    }

    /**
     * 下单提交信息
     */
    public function place_infoOp()
    {
        $hotel_id = intval($_GET['hotel_id']);
        $spec_id  = intval($_GET['spec_id']);
        if($hotel_id>0 && $spec_id>0){
            /* @var hotelModel $hotel_model */
            $hotel_model = Model('hotel');
            $hotel_info = $hotel_model->getHotelInfo(array('id'=>$hotel_id),'hotel_name');
            /* @var specModel $spec_model */
            $spec_model = Model('spec');
            $spec_info = $spec_model->getSpecInfo(array('spec_id'=>$spec_id));
            if($spec_info['parent_id']>0){
                $pt_spec_info = $spec_model->getSpecInfo(array('spec_id'=>$spec_info['parent_id']));
                Tpl::output('pt_spec_info',$pt_spec_info);
            }
            //计算价格
            $total_price = floatval($_SESSION['hotel_time']['numdate']*$spec_info['price']);
            $numdate_info = $this->prDates($_SESSION['hotel_time']['startdate'],$_SESSION['hotel_time']['enddate']);//入驻天数详情
            array_pop($numdate_info);
            /* @var order_hotelModel $order_hotel_model */
            $order_hotel_model = Model('order_hotel');
            $_where =array();
            $_where['startdate'] = array('elt',$_SESSION['hotel_time']['enddate']);
            $_where['enddate'] = array('gt',$_SESSION['hotel_time']['startdate']);
            $_where['status'] = 1;
            $_where['spec_id'] = $spec_info['parent_id']>0?$pt_spec_info['spec_id']:$spec_id;
            $hotel_oh_num = $order_hotel_model->getOrderHoteloh_num($_where);
            if($hotel_oh_num['sum_on_num']>0){
                //计算还剩多少间房间
                if($spec_info['parent_id']>0){
                    $sum_on_num = $pt_spec_info['stock']-$hotel_oh_num['sum_on_num'];
                }else{
                    $sum_on_num = $spec_info['stock']-$hotel_oh_num['sum_on_num'];
                }
                Tpl::output('sum_on_num',$sum_on_num);
            }else{
                Tpl::output('sum_on_num',$spec_info['parent_id']>0?$pt_spec_info['stock']:$spec_info['stock']);
            }

            Tpl::output('hotel_name',$hotel_info['hotel_name']);//酒店名称
            Tpl::output('hotel_time',$_SESSION['hotel_time']);
            Tpl::output('numdate_info',$numdate_info);//入驻天数详情
            Tpl::output('numdate',$_SESSION['hotel_time']['numdate']);//入驻总天数
            Tpl::output('total_price',$total_price);
            Tpl::output('spec_info',$spec_info);
            Tpl::showpage('place.info');
        }else{
            showMessage('非法操作');
        }

    }

    function prDates($start,$end){
        $dt_start = strtotime($start);
        $dt_end = strtotime($end);
        $arr = array();
        while ($dt_start<=$dt_end){
            $arr[] = date('Y-m-d',$dt_start);
             $dt_start = strtotime('+1 day',$dt_start);
        }
        return $arr;
    }

    /**
     * 选择姓名电话等
     */
    public function sel_infoOp()
    {
        /* @var member_infoModel $member_info_model */
        $member_info_model = Model('member_info');
        $infos = $member_info_model->getMemberInfoList(array('member_id'=>$_SESSION['member']['member_id']));
        Tpl::output('infos', $infos);
        Tpl::showpage('hotel.selinfo', 'null_layout');
    }

}